package com.zzz.gp.service;

import com.zzz.gp.bean.GpCodeHistory;
import com.zzz.gp.bean.GpHistory;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: ZZZ
 * @time: 2022/1/26 14:03
 */
@Slf4j
@Component
public class CommonService {
    @Value("${QC}")
    private String qc;

    @Autowired
    private GpMapper gpMapper;


    public Set<String> monthData() {
        LocalDateTime s = LocalDateTime.now();
        Set<String> result = new HashSet<>();
        List<GpCodeHistory> monthData = gpMapper.getMonthData();
        for (GpCodeHistory gpCodeHistory : monthData) {
            String code = gpCodeHistory.getCode();
            List<GpHistory> list = gpCodeHistory.getList();
            if (list.size() == 5) {
                // a b c d  例如：1月 2月 3月 4月
                double a = (list.get(1).getClosePrice() - list.get(0).getClosePrice()) / list.get(0).getClosePrice() * 100;
                double b = (list.get(2).getClosePrice() - list.get(1).getClosePrice()) / list.get(1).getClosePrice() * 100;
                double c = (list.get(3).getClosePrice() - list.get(2).getClosePrice()) / list.get(2).getClosePrice() * 100;
                double d = (list.get(4).getClosePrice() - list.get(3).getClosePrice()) / list.get(3).getClosePrice() * 100;


                //前两个月不能大跌和大涨
                if (c >= 5 || c <= -5 || d >= 5 || d <= -5) {
                    continue;
                }
                //月线差不多持平
                double z = a + b + c + d;
                if (z >= -2 && z <= 2) {
                    result.add(code);
                }
            }
        }

        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("周期月统计 用时{}秒 符合数据{}条", seconds, result.size());
        return result;
    }


    public Set<String> weekData() {
        LocalDateTime s = LocalDateTime.now();
        Set<String> result = new HashSet<>();
        List<GpCodeHistory> monthData = gpMapper.getWeekData();
        for (GpCodeHistory gpCodeHistory : monthData) {
            String code = gpCodeHistory.getCode();
            List<GpHistory> list = gpCodeHistory.getList();
            if (list.size() == 5) {
                // a b c d  例如：上上上上周  上上上周  上上周  上周
                double a = (list.get(3).getClosePrice() - list.get(4).getClosePrice()) / list.get(4).getClosePrice() * 100;
                double b = (list.get(2).getClosePrice() - list.get(3).getClosePrice()) / list.get(3).getClosePrice() * 100;
                double c = (list.get(1).getClosePrice() - list.get(2).getClosePrice()) / list.get(2).getClosePrice() * 100;
                double d = (list.get(0).getClosePrice() - list.get(1).getClosePrice()) / list.get(1).getClosePrice() * 100;


                //前两周不能大跌和大涨
                if (c >= 3 || c <= -3 || d >= 3 || d <= -3) {
                    continue;
                }
                //周线差不多持平
                double z = a + b + c + d;
                if (z >= -3 && z <= 3) {
                    result.add(code);
                }
            }
        }

        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("周期周统计 用时{}秒 符合数据{}条", seconds, result.size());
        return result;
    }


    public Set<String> dayData() {
        LocalDateTime s = LocalDateTime.now();
        Set<String> result = new HashSet<>();
        List<GpCodeHistory> monthData = gpMapper.getDayData();
        for (GpCodeHistory gpCodeHistory : monthData) {
            String code = gpCodeHistory.getCode();
            List<GpHistory> list = gpCodeHistory.getList();
            if (list.size() == 10) {

                double avg = 0;
                for (GpHistory gpHistory : list) {
                    double closePrice = gpHistory.getClosePrice();
                    avg = avg + closePrice;
                }
                avg = avg / 10;

                double p0 = Double.parseDouble(list.get(0).getIncrease_p());
                double p9 = Double.parseDouble(list.get(9).getIncrease_p());

                if (p0 > 7 || p9 > 2) {
                    continue;
                }

                if (list.get(8).getClosePrice() > avg) {
                    continue;
                }

                if (list.get(9).getClosePrice() > avg) {
                    result.add(code);
                }
            }
        }

        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("日期周统计 用时{}秒 符合数据{}条", seconds, result.size());
        return result;
    }


    public void qc() {
        exe("All", "V17", qc + "/gp/qc/V17.txt", qc + "/gp/qc/A.txt");
        exe("All", "A17", qc + "/gp/qc/A17.txt", qc + "/gp/qc/A.txt");
        exe("All", "A1", qc + "/gp/qc/A1.txt", qc + "/gp/qc/A.txt");
        exe("All", "A5", qc + "/gp/qc/A5.txt", qc + "/gp/qc/A.txt");
    }

    public void exe(String s1, String s2, String p1, String p2) {
        List<String> list = compare(s1, s2, p1, AV(p2));
        StringBuilder sb = new StringBuilder("");
        list.forEach(x -> {
            sb.append(",").append(x);
        });
        FileUtil.txtWriter(p1, sb.toString());
    }

    public List<String> compare(String s1, String s2, String filePath, List<String> old) {
        List<String> list = new ArrayList<>();
        String result = "";
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filePath);
            InputStreamReader reader = new InputStreamReader(fin);
            BufferedReader buffReader = new BufferedReader(reader);
            String strTmp = "";
            //AV 都在一行
            while ((strTmp = buffReader.readLine()) != null) {
                result = strTmp;
                System.out.println(result);
            }
            buffReader.close();
        } catch (Exception e) {
            System.out.println(filePath + "  文件读取异常");
        }
        String[] split = result.split(",");
        for (String s : split) {
            String code = s.trim();
            if (code.length() == 6) {
                list.add(code);
            }
        }

        System.out.print(s1 + "=" + old.size() + "  ");
        System.out.print(s2 + "=" + list.size() + "  ");

        for (String s : old) {
            if (list.contains(s)) {
                list.remove(s);
            }
        }
        System.out.println(s2 + "=" + list.size());
        System.out.println("===================================");
        return list;
    }


    public List<String> AV(String filePath) {
        List<String> list = new ArrayList<>();
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filePath);
            InputStreamReader reader = new InputStreamReader(fin);
            BufferedReader buffReader = new BufferedReader(reader);
            String strTmp = "";
            //AV 有多行，按照行读
            while ((strTmp = buffReader.readLine()) != null) {
                String code = strTmp.trim();
                if (code.length() == 6) {
                    list.add(code);
                }
            }
            buffReader.close();
        } catch (Exception e) {
            System.out.println(filePath + "  文件读取异常");
        }
        return list;
    }
}
